package cn.neo.support.syllabus.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.neo.support.R;
import cn.neo.support.syllabus.entity.ClassCourse;
import cn.neo.support.syllabus.entity.ClassTime;
import cn.neo.support.syllabus.entity.ViewBuildData;

/**
 * Created by Liangchen Luo on 16/2/11
 * Fork by neo
 *
 * @author neo
 */
public class LiteSyllabusView extends LinearLayout {

    public static final String[] WEEKDAY_TITLES = {"周一", "周二", "周三", "周四", "周五", "周六", "周天"};
    public static final String[] SECTION_TITLES = {"10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00"};
    private static final int[] COURSE_BACKGROUND_COLOR_RES_DEFAULT = {
            R.color.bg_1,
            R.color.bg_2,
            R.color.bg_3,
            R.color.bg_4,
            R.color.bg_5,
            R.color.bg_6,
            R.color.bg_7,
            R.color.bg_8,
            R.color.bg_9,
            R.color.bg_10,
            R.color.bg_11,
            R.color.bg_12,
            R.color.bg_13,
            R.color.bg_14,
            R.color.bg_15
    };
    private static final int HEIGHT_DEFAULT = 580;
    private static final int HEADER_HEIGHT_DEFAULT = 24;
    private static final int SIDEBAR_WIDTH_DEFAULT = 24;
    private static final float DIVIDER_WIDTH_DEFAULT = 0.5f;
    private static final float DIVIDER_HEIGHT_DEFAULT = 0.5f;
    private static final int WEEKDAY_NUMBER_DEFAULT = 7;
    private static final int WEEKDAY_NUMBER_WITHOUT_WEEKEND = 5;
    private static final int SECTION_NUMBER_DEFAULT = 10;
    private static final int WEEKDAY_TITLE_TEXT_SIZE = 14;
    private static final int SECTION_SIDEBAR_TEXT_SIZE = 7;

    private Context mContext;

    private int mWeekdayNumber;
    private int mSectionNumber;

    private int mWeekPosition;//第几周 从1开始
    private int dayOfWeek;

    private int mWidth;
    private int mHeight;
    private int mSectionWidth;
    private int mSectionHeight;
    private int mWeekdayHeaderHeight;
    private int mSectionSidebarWidth;
    private int mDividerWidth;
    private int mDividerHeight;
    private Typeface mTitleTypeface;

    private int mCourseNameTextSize;
    private int mCoursePositionTextSize;
    private int mCourseNoteTextSize;

    private LinearLayout layoutWeekdayHeader;
    private LinearLayout layoutSectionSidebar;
    private LinearLayout layoutCourseTable;

    private OnBlankViewClickListener mOnBlankViewClickListener;
    private OnViewClickListener mOnViewClickListener;

    private List<ClassCourse> mDataSource = new ArrayList<>();//数据源
    private int[] mCourseBackgroundColorPalette;

    public interface OnBlankViewClickListener {
        void onClick(View view, int weekday, int section);

        void onLongClick(View view, int weekday, int section);
    }

    public interface OnViewClickListener {
        void onClick(View view, ClassCourse classCourse, int weekday);

        void onLongClick(View view, int weekday);
    }

    public LiteSyllabusView(Context context) {
        this(context, null);
    }

    public LiteSyllabusView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public LiteSyllabusView(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
        mContext = context;
        mWeekdayNumber = WEEKDAY_NUMBER_DEFAULT;
        mSectionNumber = SECTION_NUMBER_DEFAULT;
        mWidth = getScreenWidth();
        mHeight = dip2px(HEIGHT_DEFAULT);
        mDividerWidth = dip2px(DIVIDER_WIDTH_DEFAULT);
        mDividerHeight = dip2px(DIVIDER_HEIGHT_DEFAULT);
        mWeekdayHeaderHeight = dip2px(HEADER_HEIGHT_DEFAULT);
        mSectionSidebarWidth = dip2px(SIDEBAR_WIDTH_DEFAULT);
        mCourseNameTextSize = CourseView.NAME_TEXT_SIZE_DEFAULT;
        mCoursePositionTextSize = CourseView.POSITION_TEXT_SIZE_DEFAULT;
        mCourseNoteTextSize = CourseView.NOTE_TEXT_SIZE_DEFAULT;
        try {
            mTitleTypeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/ChalkboardSE.ttc");
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        mOnBlankViewClickListener = new OnBlankViewClickListener() {
            @Override
            public void onClick(View view, int weekday, int section) {
            }

            @Override
            public void onLongClick(View view, int weekday, int section) {
            }

        };
        mOnViewClickListener = new OnViewClickListener() {

            @Override
            public void onClick(View view, ClassCourse classCourse, int weekday) {

            }

            @Override
            public void onLongClick(View view, int weekday) {

            }
        };
        setDefaultColorPalette();
        setOrientation(VERTICAL);
    }

    public void clear() {
        mDataSource.clear();
    }

    public List<ClassCourse> getDataSource() {
        return mDataSource;
    }

    public void show() {
        calculateSectionSize();
        generateCourseBackground();
        setupView();
        invalidate();
    }

    public void setCourses(List<ClassCourse> data) {
        mDataSource.clear();
        if (data == null) return;
        mDataSource.addAll(data);
    }

    public void addCourse(ClassCourse liteCourse) {
        if (liteCourse == null) return;
//        if (isCourseSectionRepeatConflict(liteCourse)) {
//            Toast.makeText(mContext, "课程有冲突。。", Toast.LENGTH_SHORT).show();
//        } else {
        mDataSource.add(liteCourse);
//        }
    }

    public void removeCourseById(String id) {
        for (ClassCourse liteCourse : mDataSource) {
            if (id.equals(liteCourse.getId())) mDataSource.remove(liteCourse);
        }
    }

    public void setSectionNumber(int sectionNumber) throws IllegalArgumentException {
        if (sectionNumber <= 0)
            throw new IllegalArgumentException("Section number must be positive");
        mSectionNumber = sectionNumber;
    }

    public int getWeekPosition() {
        return mWeekPosition;
    }

    public void setWeekPosition(int weekPosition) {
        this.mWeekPosition = weekPosition;
    }

    public int getDayOfWeek() {
        return dayOfWeek;
    }

    public void setDayOfWeek(int dayOfWeek) {
        this.dayOfWeek = dayOfWeek;
    }

    public void setCourseBackgroundColors(int[] colors) {
        mCourseBackgroundColorPalette = colors;
    }

    public void setCourseNameTextSize(int textSize) {
        mCourseNameTextSize = textSize;
    }

    public void setCoursePositionTextSize(int textSize) {
        mCoursePositionTextSize = textSize;
    }

    public void setCourseNoteTextSize(int textSize) {
        mCourseNoteTextSize = textSize;
    }

    public void setOnBlankViewClickListener(OnBlankViewClickListener listener) {
        this.mOnBlankViewClickListener = listener;
    }

    public void setOnViewClickListener(OnViewClickListener listener) {
        this.mOnViewClickListener = listener;
    }

    public void hideWeekendColumn(boolean what) {
        if (what) {
            mWeekdayNumber = WEEKDAY_NUMBER_WITHOUT_WEEKEND;
        } else {
            mWeekdayNumber = WEEKDAY_NUMBER_DEFAULT;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    private void setDefaultColorPalette() {
        int size = COURSE_BACKGROUND_COLOR_RES_DEFAULT.length;
        mCourseBackgroundColorPalette = new int[size];
        for (int i = 0; i < size; ++i) {
            int color = ContextCompat.getColor(mContext, COURSE_BACKGROUND_COLOR_RES_DEFAULT[i]);
            mCourseBackgroundColorPalette[i] = color;
        }
    }

    private void calculateSectionSize() {
        mSectionWidth = (mWidth - mSectionSidebarWidth - mWeekdayNumber * mDividerWidth) / mWeekdayNumber;
        mSectionHeight = (mHeight - mWeekdayHeaderHeight - mSectionNumber * mDividerHeight) / mSectionNumber;
    }

    private void generateCourseBackground() {
        int currentColorIndex = 0;
        int paletteSize = mCourseBackgroundColorPalette.length;
        Map<String, Integer> nameColorMap = new HashMap<>();
        if (mDataSource == null) mDataSource = new ArrayList<>();
        for (ClassCourse liteCourse : mDataSource) {
            if (nameColorMap.containsKey(liteCourse.getName())) {
                liteCourse.setBackgroundColor(nameColorMap.get(liteCourse.getName()));
            } else {
                int color = mCourseBackgroundColorPalette[currentColorIndex % paletteSize];
                nameColorMap.put(liteCourse.getName(), color);
                liteCourse.setBackgroundColor(color);
                currentColorIndex++;
            }
        }
    }

    private void setupView() {
        removeAllViews();
        setupWeekdayHeader();
        setupSectionSidebar();
        setupCourseTable();

        LinearLayout layoutBottom = new LinearLayout(mContext);
        layoutBottom.setOrientation(HORIZONTAL);
        layoutBottom.addView(layoutSectionSidebar);
        layoutBottom.addView(layoutCourseTable);

        addView(layoutWeekdayHeader);
        addView(layoutBottom);
    }

    private void setupWeekdayHeader() {
        layoutWeekdayHeader = new LinearLayout(mContext);
        layoutWeekdayHeader.setOrientation(HORIZONTAL);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mWeekdayHeaderHeight);
        layoutWeekdayHeader.setLayoutParams(params);

        TextView tvEmptyCellOnTopAndLeft = new TextView(mContext);
        tvEmptyCellOnTopAndLeft.setWidth(mSectionSidebarWidth);
        tvEmptyCellOnTopAndLeft.setHeight(mWeekdayHeaderHeight);
        tvEmptyCellOnTopAndLeft.setText("\\");
        tvEmptyCellOnTopAndLeft.setTextSize(WEEKDAY_TITLE_TEXT_SIZE);
        tvEmptyCellOnTopAndLeft.setTextColor(ContextCompat.getColor(mContext, R.color.textTitle));
        tvEmptyCellOnTopAndLeft.setGravity(Gravity.CENTER);
        tvEmptyCellOnTopAndLeft.setTypeface(mTitleTypeface);
        layoutWeekdayHeader.addView(tvEmptyCellOnTopAndLeft);

        for (int i = 0; i < mWeekdayNumber; ++i) {
            layoutWeekdayHeader.addView(getVerticalDividerView());
            TextView tvWeekdayTitle = new TextView(mContext);
            tvWeekdayTitle.setWidth(mSectionWidth);
            tvWeekdayTitle.setHeight(mWeekdayHeaderHeight);
            tvWeekdayTitle.setText(WEEKDAY_TITLES[i]);
            tvWeekdayTitle.setTextSize(WEEKDAY_TITLE_TEXT_SIZE);
            tvWeekdayTitle.setTextColor(ContextCompat.getColor(mContext, R.color.textTitle));
            tvWeekdayTitle.setGravity(Gravity.CENTER);
            tvWeekdayTitle.setTypeface(mTitleTypeface);
            layoutWeekdayHeader.addView(tvWeekdayTitle);
        }

        layoutWeekdayHeader.setBackgroundColor(ContextCompat.getColor(mContext, R.color.bgWeekdayHeader));
    }

    private void setupSectionSidebar() {
        layoutSectionSidebar = new LinearLayout(mContext);
        layoutSectionSidebar.setOrientation(VERTICAL);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(mSectionSidebarWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutSectionSidebar.setLayoutParams(params);

        for (int i = 1; i <= mSectionNumber; ++i) {
            layoutSectionSidebar.addView(getHorizontalDividerView());
            TextView tvSectionNumberTitle = new TextView(mContext);
            tvSectionNumberTitle.setWidth(mSectionSidebarWidth);
            tvSectionNumberTitle.setHeight(mSectionHeight);
            tvSectionNumberTitle.setText("" + i + "\n" + SECTION_TITLES[i - 1]);
            tvSectionNumberTitle.setTextSize(SECTION_SIDEBAR_TEXT_SIZE);
            tvSectionNumberTitle.setTextColor(ContextCompat.getColor(mContext, R.color.textTitle));
            tvSectionNumberTitle.setGravity(Gravity.CENTER);
            tvSectionNumberTitle.setTypeface(mTitleTypeface);
            layoutSectionSidebar.addView(tvSectionNumberTitle);
        }

        layoutSectionSidebar.setBackgroundColor(ContextCompat.getColor(mContext, R.color.bgSectionSidbar));
    }

    private void setupCourseTable() {
        layoutCourseTable = new LinearLayout(mContext);
        layoutCourseTable.setOrientation(HORIZONTAL);
        /*创建7天的课程*/
        for (int i = 0; i < mWeekdayNumber; ++i) {
            layoutCourseTable.addView(getVerticalDividerView());
            layoutCourseTable.addView(getCourseViewColumn(i));
        }
    }

    /**
     * 创建一天的课程
     *
     * @param weekday 当前日期
     * @return 当前日期的课程
     * @throws InvalidParameterException
     */
    private LinearLayout getCourseViewColumn(final int weekday) throws InvalidParameterException {
        //创建当前日期的root层
        LinearLayout layoutColumn = new LinearLayout(mContext);
        layoutColumn.setOrientation(VERTICAL);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(mSectionWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutColumn.setLayoutParams(params);
        //获取当前日期的课程
        List<ViewBuildData> coursesInCurrentDay = getViewBuildDatas4CurrentDay(weekday);
        if (mDataSource == null) coursesInCurrentDay = new ArrayList<>();

        //下面使用BuildData
        //根据，起始“节数”排序，从小到大
        Collections.sort(coursesInCurrentDay, new Comparator<ViewBuildData>() {
            @Override
            public int compare(ViewBuildData lhs, ViewBuildData rhs) {
                return Integer.valueOf(lhs.getStartSection()).compareTo(rhs.getStartSection());
            }
        });
        //如果下一个课程的起始日期小于或等于上一个结束，那么抛出错误
        for (int i = 1; i < coursesInCurrentDay.size(); ++i) {
            if (coursesInCurrentDay.get(i).getStartSection() <= coursesInCurrentDay.get(i - 1).getEndSection()) {
                throw new InvalidParameterException("CN：课程时间冲突（EN：There are duration conflicts among courses）");
            }
        }
        for (int i = 0; i < coursesInCurrentDay.size(); ++i) {
            int blankSectionStartPosition;
            final ViewBuildData viewBuildData = coursesInCurrentDay.get(i);
            if (i == 0) {
                blankSectionStartPosition = 1;
            } else {
                blankSectionStartPosition = coursesInCurrentDay.get(i - 1).getEndSection() + 1;
            }
            // 添加每节课前空课时View
            for (int j = blankSectionStartPosition; j < viewBuildData.getStartSection(); ++j) {
                layoutColumn.addView(getHorizontalDividerView());
                final int section = j;
                CourseView blankCourseView = new CourseView.Builder(mContext)
                        .setSectionWidth(mSectionWidth)
                        .setSectionHeight(mSectionHeight)
                        .setSection(section)
                        .setWeekday(weekday)
                        .setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mOnBlankViewClickListener.onClick(v, weekday, section);
                            }
                        })
                        .setOnLongClickListener(new OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {
                                mOnBlankViewClickListener.onLongClick(v, weekday, section);
                                return true;
                            }
                        })
                        .createBlankSection();
                layoutColumn.addView(blankCourseView);
            }
            // 添加position为i的课的View
            layoutColumn.addView(getHorizontalDividerView());
            CourseView courseView = new CourseView.Builder(mContext)
                    .setSectionWidth(mSectionWidth)
                    .setSectionHeight(mSectionHeight)
                    .setName(viewBuildData.getName())
                    .setSite(viewBuildData.getSite())
                    .setLecturer(viewBuildData.getLecturer())
                    .setNameTextSize(mCourseNameTextSize)
                    .setPositionTextSize(mCoursePositionTextSize)
                    .setNoteTextSize(mCourseNoteTextSize)
                    .setStartSection(viewBuildData.getStartSection())
                    .setEndSection(viewBuildData.getEndSection())
                    .setWeekday(weekday)
                    .setBackgroundColor(viewBuildData.getBackgroundColor())
                    .setActive(viewBuildData.isActive())
                    .setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnViewClickListener.onClick(v, viewBuildData.getData(), weekday);
                        }
                    })
                    .setOnLongClickListener(new OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            mOnViewClickListener.onLongClick(v, weekday);
                            return true;
                        }
                    })
                    .create();
            courseView.setTag(viewBuildData);
            FrameLayout courseFrame = new FrameLayout(mContext);
            courseFrame.addView(courseView);
            courseFrame.setBackgroundResource(R.drawable.litesyllabusview_shade);
            layoutColumn.addView(courseFrame);
            // 如果是当天最后一节课，添加末尾的空课时View
            if (i == coursesInCurrentDay.size() - 1) {
                blankSectionStartPosition = viewBuildData.getEndSection() + 1;
                for (int j = blankSectionStartPosition; j <= mSectionNumber; ++j) {
                    layoutColumn.addView(getHorizontalDividerView());
                    final int section = j;
                    CourseView blankCourseView = new CourseView.Builder(mContext)
                            .setSectionWidth(mSectionWidth)
                            .setSectionHeight(mSectionHeight)
                            .setSection(section)
                            .setWeekday(weekday)
                            .setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mOnBlankViewClickListener.onClick(v, weekday, section);
                                }
                            })
                            .setOnLongClickListener(new OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View v) {
                                    mOnBlankViewClickListener.onLongClick(v, weekday, section);
                                    return true;
                                }
                            })
                            .createBlankSection();
                    layoutColumn.addView(blankCourseView);
                }
            }
        }
        //如果当天没有课程
        if (coursesInCurrentDay.isEmpty()) {
            for (int i = 1; i <= mSectionNumber; ++i) {
                layoutColumn.addView(getHorizontalDividerView());
                final int section = i;
                CourseView blankCourseView = new CourseView.Builder(mContext)
                        .setSectionWidth(mSectionWidth)
                        .setSectionHeight(mSectionHeight)
                        .setSection(section)
                        .setWeekday(weekday)
                        .setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mOnBlankViewClickListener.onClick(v, weekday, section);
                            }
                        })
                        .setOnLongClickListener(new OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {
                                mOnBlankViewClickListener.onLongClick(v, weekday, section);
                                return true;
                            }
                        })
                        .createBlankSection();
                layoutColumn.addView(blankCourseView);
            }
        }
        return layoutColumn;
    }

    /**
     * 筛选当周的课程
     *
     * @return 当周的课程
     */
    private List<ClassCourse> getCurrentWeek() {
        List<ClassCourse> currentWeeks = new ArrayList<>();
        for (ClassCourse classCourse : mDataSource) {
            List<ClassTime> classTimes = classCourse.getInfo();
            out:
            for (ClassTime classTime : classTimes) {
                String strWeek = classTime.getWeeks();
                String[] weeks = strWeek.split(",");
                for (String week : weeks) {
                    if (TextUtils.equals(week, String.valueOf(mWeekPosition))) {
                        currentWeeks.add(classCourse);
                        break out;
                    }
                }
            }

        }
        return currentWeeks;
    }

    /**
     * 筛选当天的课程
     *
     * @param weekdayNumber 当前天
     * @return 当天的课程
     */
    private List<ViewBuildData> getViewBuildDatas4CurrentDay(int weekdayNumber) {
        List<ViewBuildData> viewBuildDatas = new ArrayList<>();
        for (ClassCourse classCourse : mDataSource) {
            List<ClassTime> classTimes = classCourse.getInfo();
            for (ClassTime classTime : classTimes) {
                String strWeek = classTime.getWeeks();
                String[] weeks = strWeek.split(",");
                //通过当前天
                if (TextUtils.equals(classTime.getWeekTime(), String.valueOf(weekdayNumber))) {
                    for (String week : weeks) {
                        //通过当前周
                        if (TextUtils.equals(week, String.valueOf(mWeekPosition))) {
                            ViewBuildData viewBuildData = classCourse2ViewBuildData(classCourse, classTime);
                            viewBuildDatas.add(viewBuildData);
                        }
                    }

                }
            }
        }
        return viewBuildDatas;
    }

    /**
     * 课程实体转试图创建数据实体
     *
     * @param classCourse 课程实体
     * @param classTime   时间段实体
     * @return 创建数据实体
     */
    private ViewBuildData classCourse2ViewBuildData(ClassCourse classCourse, ClassTime classTime) {
        ViewBuildData viewBuildData = new ViewBuildData();
        viewBuildData.setName(classCourse.getName());
        viewBuildData.setLecturer(classCourse.getLecturer());
        viewBuildData.setSite(classTime.getSite());
        viewBuildData.setStartSection(Integer.valueOf(classTime.getStartTime()));
        viewBuildData.setEndSection(Integer.valueOf(classTime.getEndTime()));
        viewBuildData.setData(classCourse);
        viewBuildData.setBackgroundColor(classCourse.getBackgroundColor());
        return viewBuildData;
    }

    /**
     * 课程冲突检测
     *
     * @param insertCourse 课程实体
     * @return true 有冲突 false 没有冲突
     */
    private boolean isCourseSectionRepeatConflict(ClassCourse insertCourse) {
        //获取当前日期的课程  重写此方法
//        List<ClassCourse> coursesInCurrentDay = new ArrayList<>();
//        if (mDataSource == null) mDataSource = new ArrayList<>();
//        coursesInCurrentDay.add(insertCourse);
//        for (ClassCourse liteCourse : mDataSource) {
//            if (liteCourse.getWeekday() == insertCourse.getWeekday()) {
//                coursesInCurrentDay.add(liteCourse);
//            }
//        }
//
//        //根据，起始“节数”排序，从小到大
//        Collections.sort(coursesInCurrentDay, new Comparator<ClassCourse>() {
//            @Override
//            public int compare(ClassCourse lhs, ClassCourse rhs) {
//                return Integer.valueOf(lhs.getStartSection()).compareTo(rhs.getStartSection());
//            }
//        });
//        //插入的课程的位置
//        int position = coursesInCurrentDay.indexOf(insertCourse);
//        if (insertCourse.getStartSection() <= coursesInCurrentDay.get(position - 1).getEndSection() ||
//                insertCourse.getEndSection() >= coursesInCurrentDay.get(position + 1).getStartSection()) {
//            //CN：课程时间冲突（EN：There are duration conflicts among courses）
//            return true;
//        }
        return false;
    }

    private View getVerticalDividerView() {
        View divider = new View(mContext);
        divider.setBackgroundColor(ContextCompat.getColor(mContext, R.color.divider));
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(mDividerWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        divider.setLayoutParams(params);
        return divider;
    }

    private View getHorizontalDividerView() {
        View divider = new View(mContext);
        divider.setBackgroundColor(ContextCompat.getColor(mContext, R.color.divider));
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mDividerHeight);
        divider.setLayoutParams(params);
        return divider;
    }

    private int getScreenWidth() {
        return mContext.getResources().getDisplayMetrics().widthPixels;
    }

    private int dip2px(float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
